/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.resource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.bean.CollectTaskBean;
import com.sinodata.bsm.center.bean.ResBean;
import com.sinodata.bsm.center.cache.CollectFieldCache;
import com.sinodata.bsm.center.cache.CollectorCache;
import com.sinodata.bsm.center.cache.PropertyCache;
import com.sinodata.bsm.center.message.Messenger;
import com.sinodata.bsm.center.message.ProbeMessenger;
import com.sinodata.bsm.center.service.probe.ProbeService;
import com.sinodata.bsm.center.service.resource.CollectTaskService;
import com.sinodata.bsm.center.service.resource.PropertyService;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.constants.PropertyConstants;
import com.sinodata.bsm.common.message.ServiceMessage;
import com.sinodata.bsm.common.utils.CrontabUtil;
import com.sinodata.bsm.common.vo.CollectField;
import com.sinodata.bsm.common.vo.CollectParam;
import com.sinodata.bsm.common.vo.CollectSchedule;
import com.sinodata.bsm.common.vo.CollectScheduleInmp;
import com.sinodata.bsm.common.vo.CollectTask;
import com.sinodata.bsm.common.vo.Collector;
import com.sinodata.bsm.common.vo.Probe;
import com.sinodata.bsm.common.vo.Property;
import com.sinodata.bsm.common.vo.Res;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author fanshengchao
 * @version 1.0
 * 
 * <p>
 * History:
 * 
 * Date Author Version Description
 * ---------------------------------------------------------------------------------
 * 2012-6-13 下午02:24:40 fanshengchao 1.0 To create
 * </p>
 * 
 * @since
 * @see
 */
public class CollectTaskAction extends BaseAction<CollectTask> {

    private CollectTask entity;
    private Long id;
    @Autowired
    private CollectTaskService collectTaskService;
    @Autowired
    private PropertyCache propertyCache;
    @Autowired
    private CollectorCache collectorCache;
    @Autowired
    private PropertyService propertyService;
    @Autowired
    private CollectFieldCache collectFieldCache;
    @Autowired
    private ProbeService probeService;

    /*
     * (non-Javadoc)
     * 
     * @see com.sinodata.bsm.center.web.action.BaseAction#delete()
     */
    @Override
    public String delete() throws Exception {
        collectTaskService.deleteCollectTask(id);
        return null;
    }

    public String inputLog() {
        String method = "contfilt";
        System.out.println(method);
        if (method == null || method.length() < 1) {
            return null;
        }
        if ("linefilt".equals(method)) {

        } else if ("contfilt".equals(method)) {
            return "contfilt";
        } else if ("statis".equals(method)) {

        }

        return null;
    }

    @Override
    public String input() {
        try {
            Long collectId = Long.parseLong(Struts2Utils.getParameter("id"));
            Long resId = Long.parseLong(Struts2Utils.getParameter("resId"));

            /*------------------------------------*/
            //自定义指标
            List<Property> propertys = new ArrayList<Property>();
            List<Property> customPropertys = propertyService.findPropertyByResId(resId);

            //已经被占用的字段
            List<CollectField> otherField = new ArrayList<CollectField>();
            List<CollectTask> currentResTask = collectTaskService.findByResId(resId);
            for (int i = 0; currentResTask != null && i < currentResTask.size(); i++) {
                CollectTask task = currentResTask.get(i);
                List<CollectField> currentField = collectFieldCache.get(task.getId());
                for (int c = 0; currentField != null && c < currentField.size(); c++) {
                    CollectField field = currentField.get(c);
                    if (field.getFieldName() != null && !field.getFieldName().equals(""))
                        otherField.add(field);
                }
            }

            //页面是否显示自定义指标
            for (int t = 0; customPropertys != null && t < customPropertys.size(); t++) {
                Property p = customPropertys.get(t);
                boolean addFlag = true;
                for (int i = 0; i < otherField.size(); i++) {
                    CollectField field = otherField.get(i);
                    if (field.getProp().longValue() == p.getId().longValue() && field.getFieldName() != null) {
                        addFlag = false;
                    }
                }

                if (addFlag) {
                    propertys.add(p);
                }
            }

            //所有的字段
            List<CollectField> allField = new ArrayList<CollectField>();
            List<CollectTask> currentTypeTask = collectTaskService.findByResTypeId(ResBean.get(resId).res().getResTypeId());
            for (int i = 0; currentTypeTask != null && i < currentTypeTask.size(); i++) {
                CollectTask task = currentTypeTask.get(i);
                List<CollectField> currentField = CollectTaskBean.get(task.getId()).getCollectField();
                for (int c = 0; currentField != null && c < currentField.size(); c++) {
                    CollectField field = currentField.get(c);
                    if (field.getFieldName() == null)
                        allField.add(field);
                }
            }

            for (int i = 0; i < allField.size(); i++) {
                CollectField field = allField.get(i);
                boolean addFlag = true;
                for (int t = 0; t < otherField.size(); t++) {
                    CollectField tempField = otherField.get(t);
                    if (field.getProp().longValue() == tempField.getProp().longValue() && field.getFieldName() != null) {
                        addFlag = false;
                        allField.remove(i);
                    }
                }
                if (addFlag)
                    propertys.add(propertyService.getProperty(field.getProp()));
            }

            /*------------------------------------*/
            Collector collector = collectorCache.get(collectId);
            com.sinodata.bsm.cicp.collector.Collector impleColl = (com.sinodata.bsm.cicp.collector.Collector) Class.forName(collector.getClassName()).newInstance();
            String[] params = impleColl.getParamMeta();

            String[] props = null;
            try {
                props = impleColl.getPropMeta();
            } catch (Exception e) {
                // TODO: handle exception
            }
            Struts2Utils.setRequestAttribute("resId", resId);
            Struts2Utils.setRequestAttribute("collector", collector);
            Struts2Utils.setRequestAttribute("props", props == null ? new String[] {} : props);
            Struts2Utils.setRequestAttribute("params", params == null ? new String[] {} : params);
            Struts2Utils.setRequestAttribute("propertys", propertys);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "input";
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sinodata.bsm.center.web.action.BaseAction#list()
     */
    @Override
    public String list() {
        Long resId = Long.parseLong(Struts2Utils.getParameter("resId"));
        List<CollectTask> collctparams = collectTaskService.findByResId(resId);// 采集器实例
        List<CollectTask> showList = new ArrayList<CollectTask>(); // 显示的结果
        List<CollectTask> typelist = collectTaskService.findByResTypeId(ResBean.get(resId).res().getResTypeId()); // 默认采集器实例
        if (collctparams != null) {
            showList.addAll(collctparams);
            for (CollectTask task : typelist == null ? typelist = new ArrayList<CollectTask>() : typelist) {
                boolean hasConf = false;
                for (CollectTask task2 : collctparams) {
                    if (task2.getResTypeTaskId() != null && task.getId().longValue() == task2.getResTypeTaskId().longValue()) {
                        hasConf = true;
                    }
                }
                if (!hasConf) {
                    showList.add(task);
                }
            }
        } else if (typelist != null) {
            showList.addAll(typelist);
        }

        JSONArray tasks = new JSONArray();
        JSONObject json = new JSONObject();

        for (int i = 0; i < showList.size(); i++) {
            CollectTask collectTask = showList.get(i);

            CollectTaskBean taskBean = CollectTaskBean.get(collectTask.getId());
            Collector collector = taskBean.getCollector();
            JSONObject task = new JSONObject();
            task.element("operate", "");
            task.element("id", collector == null ? "" : collectTask.getId());
            task.element("resId", collectTask.getResId() == null ? "" : collectTask.getResId());
            task.element("collectorName", collector.getName());
            // 采集指标
            List<CollectField> fields = taskBean.getCollectField();
            StringBuffer propertyValues = new StringBuffer("");
            for (int index = 0; fields != null && index < fields.size(); index++) {
                CollectField field = fields.get(index);
                Property property = propertyCache.get(field.getProp());
                propertyValues.append(property == null ? "" : (property.getName()));
                if (index != fields.size() - 1) {
                    propertyValues.append(",");
                }
            }
            task.element("property", propertyValues.toString());

            // 调度方式
            List<CollectSchedule> schedules = taskBean.getCollectSchedule() == null ? new ArrayList<CollectSchedule>() : taskBean.getCollectSchedule();
            StringBuffer scheduleValues = new StringBuffer("");
            for (CollectSchedule schedule : schedules) {
                String scheduleName = schedule.getScheduleType().intValue() == PropertyConstants.PROPERTY_REFRESH_PERIOD ? "周期采集:" : "定时采集:";
                String scheduleValue = "";
                if (schedule.getScheduleType().intValue() == PropertyConstants.PROPERTY_REFRESH_PERIOD) {
                    scheduleValue = CrontabUtil.parseSimple(Integer.parseInt(schedule.getScheduleParam()));
                } else {
                    scheduleValue = CrontabUtil.CronParser(schedule.getScheduleParam().toString());
                }
                scheduleValues.append(scheduleName + scheduleValue + "</br>");
            }
            task.element("schedule", scheduleValues.toString());
            task.element("isSave", collectTask.getIsSave().intValue() == 1 ? "是" : "否");
            task.element("status", collectTask.getStatus().intValue() == 1 ? "启用" : "禁用");
            task.element("statusIndex", collectTask.getStatus().intValue());
            task.element("isDefault", collectTask.getResId() == null ? "是" : "否");
            tasks.add(task);
        }
        json.element("aaData", tasks);
        json.element("iTotalRecords", 0);
        json.element("iTotalDisplayRecords", 0);

        Struts2Utils.renderText(json.toString());
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sinodata.bsm.center.web.action.BaseAction#prepareModel()
     */
    @Override
    protected void prepareModel() throws Exception {
        if (id == null) {
            entity = new CollectTask();
        } else {
            entity = collectTaskService.getById(id);
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sinodata.bsm.center.web.action.BaseAction#save()
     */

    @Override
    public String save() throws Exception {
        entity.setStatus(1);
        List<CollectSchedule> collectSchedules = new ArrayList<CollectSchedule>();
        List<CollectParam> collectParams = new ArrayList<CollectParam>();
        List<CollectField> collectFields = new ArrayList<CollectField>();

        JSONArray scheduleArray = JSONArray.fromObject(Struts2Utils.getParameter("collectSchedules"));
        CollectSchedule[] schedules = (CollectSchedule[]) JSONArray.toArray(scheduleArray, CollectSchedule.class);
        for (int i = 0; i < schedules.length; i++) {
            collectSchedules.add(schedules[i]);
        }

        String[] vals = Struts2Utils.getRequest().getParameterValues("paramValue"); // 保存paramValue
        JSONArray paramArray = JSONArray.fromObject(Struts2Utils.getParameter("paramNames"));
        CollectParam[] params = (CollectParam[]) JSONArray.toArray(paramArray, CollectParam.class);
        for (int i = 0; i < params.length; i++) {
            CollectParam param = params[i];
            param.setParamValue(vals[i]);
            collectParams.add(param);
        }

        JSONArray fieldArray = JSONArray.fromObject(Struts2Utils.getParameter("collectFields"));
        CollectField[] fields = (CollectField[]) JSONArray.toArray(fieldArray, CollectField.class);
        for (int i = 0; i < fields.length; i++) {
            collectFields.add(fields[i]);
        }

        collectTaskService.saveOrUpdateCollectTask(entity, collectSchedules, collectParams, collectFields);

        List<Collector> collectors = collectorCache.getAll();
        Struts2Utils.setRequestAttribute("collectors", collectors);
        Struts2Utils.setRequestAttribute("resId", entity.getResId());
        return "common";
    }

    public String batchSave() throws Exception {
        try {
            String idsValue = Struts2Utils.getParameter("ids");
            String[] resIds = idsValue.split(",");
            Long[] ids = new Long[resIds.length];
            for (int i = 0; i < resIds.length; i++) {
                ids[i] = Long.parseLong(resIds[i]);
            }
            Long resId = Long.parseLong(Struts2Utils.getParameter("resId"));
            collectTaskService.batchSaveCollectTask(resId, ids);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 
     * @return
     */
    public String common() {
        Long resId = Long.parseLong(Struts2Utils.getParameter("id"));

        //判断当前资源的probe是否是INMP(集成朗逸网络资源用)类型
        Res res = ResBean.get(resId).res();
        Probe probe = probeService.findById(res.getProbeId());
        if (probe.getType().equals("INMP")) {
            Struts2Utils.setRequestAttribute("res", res);
            return "netconfig";
        }

        List<Collector> collectors = collectorCache.getAll();
        Struts2Utils.setRequestAttribute("resId", resId);
        Struts2Utils.setRequestAttribute("collectors", collectors);
        return "common";
    }

    @SuppressWarnings("unchecked")
    public String initUpdate() throws Exception {
        try {
            Long resId = Long.parseLong(Struts2Utils.getParameter("resId"));
            CollectTaskBean taskBean = CollectTaskBean.get(id);

            CollectTask collecttask = taskBean.collectTask();
            Collector collector = taskBean.getCollector();

            List<CollectSchedule> schedules = taskBean.getCollectSchedule();
            List<CollectParam> params = taskBean.getCollectParam();
            if (params == null) {
                params = new ArrayList<CollectParam>(0);
            }
            Collections.sort(params, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    CollectParam c1 = (CollectParam) o1;
                    CollectParam c2 = (CollectParam) o2;
                    return c1.getParamName().hashCode() - c2.getParamName().hashCode();
                }
            });
            /*------------------------------------*/
            List<CollectField> currentField = taskBean.getCollectField();
            List<CollectTask> currentResTask = collectTaskService.findByResId(resId);
            List<Property> propertys = new ArrayList<Property>();
            //所有默认采集器
            List<CollectTask> allTask = new ArrayList<CollectTask>();
            List<CollectTask> defaultTask = collectTaskService.findByResTypeId(ResBean.get(resId).res().getResTypeId());
            if (defaultTask != null) {
                for (int i = 0; i < defaultTask.size(); i++) {
                    allTask.add(defaultTask.get(i));
                }
            }

            //未赋值的指标
            List<CollectField> allField = new ArrayList<CollectField>();
            for (int i = 0; i < allTask.size(); i++) {

                if (currentResTask == null) {
                    break;
                }

                CollectTask task = allTask.get(i);
                List<CollectField> fields = CollectTaskBean.get(task.getId()).getCollectField();
                for (int t = 0; fields != null && t < fields.size(); t++) {
                    CollectField field = fields.get(t);

                    boolean isContinue = false;
                    //筛选出默认
                    for (int k = 0; currentResTask != null && k < currentResTask.size(); k++) {
                        CollectTask tempTask = currentResTask.get(k);
                        if ((tempTask.getResTypeTaskId() != null && tempTask.getResTypeTaskId().longValue() != task.getId().longValue()) && field.getFieldName() != null || (task.getResId() == resId)) {
                            isContinue = true;
                            break;
                        }
                    }

                    if (isContinue) {
                        continue;
                    }

                    if (task.getId().longValue() != id.longValue()) {
                        boolean addFlag = true;
                        for (int c = 0; currentField != null && c < currentField.size(); c++) {
                            CollectField temp = currentField.get(c);
                            if (temp.getProp().longValue() == field.getProp().longValue()) {
                                addFlag = false;
                            }

                            if (!addFlag) {
                                break;
                            }
                        }

                        for (int l = 0; addFlag && currentResTask != null && l < currentResTask.size(); l++) {
                            CollectTask task1 = currentResTask.get(l);
                            List<CollectField> fields1 = collectFieldCache.get(task1.getId());
                            for (int k = 0; fields1 != null && k < fields1.size(); k++) {
                                CollectField temp = fields1.get(k);
                                if (temp.getProp().longValue() == field.getProp().longValue()) {
                                    addFlag = false;
                                    break;
                                }
                            }
                            if (addFlag == false) {
                                break;
                            }
                        }

                        if (addFlag) {
                            Property p = propertyService.getProperty(field.getProp());
                            propertys.add(p);
                        }
                    }
                }
            }

            //已经拥有的指标(已经赋值)
            for (int i = 0; currentField != null && i < currentField.size(); i++) {
                CollectField field = currentField.get(i);
                allField.add(field);
                Property p = propertyService.getProperty(field.getProp());
                propertys.add(p);
            }

            //自定义指标
            List<Property> customPropertys = propertyService.findPropertyByResId(resId);
            if (currentResTask != null)
                allTask.addAll(currentResTask);
            for (int c = 0; customPropertys != null && c < customPropertys.size(); c++) {
                Property p = customPropertys.get(c);
                boolean addFlag = true;
                for (int i = 0; i < allTask.size(); i++) {
                    CollectTask task = allTask.get(i);
                    List<CollectField> fields = collectFieldCache.get(task.getId());
                    for (int t = 0; fields != null && t < fields.size(); t++) {
                        CollectField tempField = fields.get(t);
                        if (p.getId().longValue() == tempField.getProp().longValue() && tempField.getFieldName() != null) {
                            addFlag = false;
                            break;
                        }
                    }
                    if (addFlag == false) {
                        break;
                    }
                }

                if (addFlag) {
                    propertys.add(p);
                }
            }

            /*------------------------------------*/
            String className = collector.getClassName();
            com.sinodata.bsm.cicp.collector.Collector c = (com.sinodata.bsm.cicp.collector.Collector) Class.forName(className).newInstance();
            Map<String, String> paramMap = new HashMap<String, String>();
            for (int i = 0; params != null && i < params.size(); i++) {
                CollectParam param = params.get(i);
                if (param != null) {
                    paramMap.put(param.getParamName(), param.getParamValue() == null ? "" : param.getParamValue());
                }
            }
            c.setParamMap(paramMap);
            String[] props = null;
            try {
                props = c.getPropMeta();
            } catch (Exception e) {

            }

            Struts2Utils.setRequestAttribute("collector", collector);
            Struts2Utils.setRequestAttribute("collecttask", collecttask);
            Struts2Utils.setRequestAttribute("schedules", schedules);
            Struts2Utils.setRequestAttribute("params", params);
            Struts2Utils.setRequestAttribute("fields", allField);
            Struts2Utils.setRequestAttribute("props", props);
            Struts2Utils.setRequestAttribute("propertys", propertys);
            Struts2Utils.setRequestAttribute("resId", resId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "update";
    }

    public void prepareUpdate() throws Exception {
        prepareModel();
    }

    /**
     * 修改采集规则 如果修改的采集规则是默认的,则将该采集规则进行添加(连同采集调度、采集参数、采集字段),并且只作用于当前资源。
     * 否则不是默认的采集规则，则只需将当前的采集规则先删除在增加。
     * 
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public String update() throws Exception {
        JSONArray scheduleArray = JSONArray.fromObject(Struts2Utils.getParameter("collectSchedules"));
        List<CollectSchedule> collectSchedules = new ArrayList<CollectSchedule>();
        CollectSchedule[] schedules = (CollectSchedule[]) JSONArray.toArray(scheduleArray, CollectSchedule.class);
        for (int i = 0; i < schedules.length; i++) {
            collectSchedules.add(schedules[i]);
        }

        String[] vals = Struts2Utils.getRequest().getParameterValues("paramValue"); // 保存paramValue
        String tempProp = Struts2Utils.getParameter("paramNames"); // 保存paramName
        JSONArray paramArray = JSONArray.fromObject(tempProp);
        CollectParam[] params = (CollectParam[]) JSONArray.toArray(paramArray, CollectParam.class);
        List<CollectParam> collectParams = new ArrayList<CollectParam>();
        for (int i = 0; vals != null && i < vals.length; i++) {
            CollectParam param = new CollectParam();
            param.setParamValue(vals[i]);
            param.setParamName(params[i].getParamName());
            collectParams.add(param);
        }

        JSONArray fieldArray = JSONArray.fromObject(Struts2Utils.getParameter("collectFields"));
        List<CollectField> collectFields = new ArrayList<CollectField>();
        CollectField[] fields = (CollectField[]) JSONArray.toArray(fieldArray, CollectField.class);
        for (int i = 0; fields != null && i < fields.length; i++) {
            collectFields.add(fields[i]);
        }

        CollectTask oldTask = collectTaskService.getById(entity.getId());
        CollectTask savedTask = null;

        // 如果是默认的采集任务 新增一条采集任务只作用当前的资源
        if (oldTask.getResTypeId() != null) {
            savedTask = oldTask.clone();
            savedTask.setResId(entity.getResId());
            savedTask.setIsSave(entity.getIsSave());
            savedTask.setResTypeTaskId(oldTask.getId());
            savedTask.setId(null);
            savedTask.setResTypeId(null);
            collectTaskService.saveOrUpdateCollectTask(false, savedTask, collectSchedules, collectParams, collectFields);
        } else {
            savedTask = oldTask;
            savedTask.setResId(entity.getResId());
            savedTask.setIsSave(entity.getIsSave());
            collectTaskService.saveOrUpdateCollectTask(true, savedTask, collectSchedules, collectParams, collectFields);
        }

        List<Collector> collectors = collectorCache.getAll();
        Struts2Utils.setRequestAttribute("collectors", collectors);
        Struts2Utils.setRequestAttribute("resId", entity.getResId());
        return "common";
    }

    /**
     * 修改采集任务状态(业务解释同update()相似) 启用:1 禁用：0
     * 
     * @return
     * @throws Exception
     */
    public String updateState() throws Exception {
        Long resId = Long.parseLong(Struts2Utils.getParameter("resId"));
        CollectTask task = collectTaskService.getById(id);
        CollectTask saveTask = null;

        List<CollectTask> typelist = collectTaskService.findByResId(resId); // 非默认采集器实例
        boolean isAdd = true;
        //更改的采集任务是否已经禁用过或者启用过
        for (CollectTask resTask : typelist) {
            if (resTask.getResTypeTaskId().longValue() == task.getId().longValue()) {
                saveTask = resTask;
                isAdd = false;
                break;
            }
        }

        // 默认的采集任务
        if (task.getResId() == null && isAdd == true) {
            saveTask = task.clone();
            saveTask.setId(null);
            saveTask.setResTypeId(null);
            saveTask.setResTypeTaskId(id);
            List<CollectParam> params = collectTaskService.getCollectParam(id);
            List<CollectParam> newparams = new ArrayList<CollectParam>();
            if (params != null) {
                for (CollectParam param : params) {
                    CollectParam newparam = param.clone();
                    newparams.add(newparam);
                }
            }

            List<CollectField> fields = collectTaskService.getCollectField(id);
            List<CollectField> newFields = new ArrayList<CollectField>();
            if (fields != null) {
                for (CollectField field : fields) {
                    CollectField newField = field.clone();
                    newFields.add(newField);
                }
            }

            List<CollectSchedule> schedules = collectTaskService.getCollectSchedule(id);
            List<CollectSchedule> newSchedules = new ArrayList<CollectSchedule>();

            if (schedules != null) {
                for (CollectSchedule schedule : schedules) {
                    CollectSchedule newSchedule = schedule.clone();
                    newSchedules.add(newSchedule);
                }
            }

            saveTask.setResId(resId);
            saveTask.setStatus(task.getStatus().intValue() == 1 ? 0 : 1);
            collectTaskService.saveOrUpdateCollectTask(saveTask, newSchedules, newparams, newFields);
        } else if (isAdd) {
            task.setStatus(task.getStatus().intValue() == 1 ? 0 : 1);
            collectTaskService.updateCollectTask(task);
            saveTask = task;
        }

        // 采集规则暂停或恢复
        ProbeMessenger.notifyCollectTaskPausedOrNormal(saveTask);
        return null;
    }

    /**
     * 查询所有采集器
     * 
     * @return
     */
    public String findCollect() {
        List<Collector> collectors = collectorCache.getAll();
        Struts2Utils.renderJson(collectors);
        return null;
    }

    /**
     * 采集实例是否删除
     * 
     * @return true:已删除 false:未删除
     */
    public String isDelete() {
        Struts2Utils.renderText(" " + (collectTaskService.getById(id) == null) + " ");
        return null;
    }

    /**
     * 保存网络集成的配置
     * @return
     */
    public String saveNetconfig() {
        JSONObject json = new JSONObject();
        try {
            Long resId = Long.parseLong(Struts2Utils.getParameter("resId"));
            String param = Struts2Utils.getParameter("param");
            CollectScheduleInmp scheduleInmp = new CollectScheduleInmp(resId, param);
            boolean flag = collectTaskService.saveOrUpdateNetconfig(scheduleInmp);
            Struts2Utils.renderJson(JSONObject.fromObject("{'flag':" + flag + "}"));
            //通知probe
            ServiceMessage msg = new ServiceMessage();
            msg.setSrvId("CollectorService");
            msg.setMethod("notifyNetCollect");
            Object[] params = new Object[2];
            params[0] = resId;
            params[1] = param;
            msg.setParams(params);
            Res res = ResBean.get(resId).res();
            // Messenger.getInstance().sendAsynMessage(res.getProbeId(), msg);

            //应该用同步消息
            Object obj = null;
            try {
                obj = Messenger.getInstance().sendSyncMessage(res.getProbeId(), msg);
            } catch (Exception e) {
                logger.error(e);
            }
            if (obj != null) {
                json.element("flag", (String) obj);
            } else {
                json.element("flag", "false");
            }
            Struts2Utils.renderText(json.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public CollectTask getModel() {
        return entity;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

}
